<?php
/**
 * PHPSense Pagination Class
 *
 * PHP tutorials and scripts
 *
 * @package		PHPSense
 * @author		Jatinder Singh Thind
 * @copyright	Copyright (c) 2006, Jatinder Singh Thind
 * @link		http://www.phpsense.com
 */

// ------------------------------------------------------------------------


class Pagination extends db{
	var $php_self;
	var $rows_per_page = 10; //Number of records to display per page
	var $total_rows = 0; //Total number of rows returned by the query
	var $links_per_page = 5; //Number of links to display per page
	var $append = ""; //Paremeters to append to pagination links
	var $sql = "";
	var $debug = false;
	var $conn = false;
	var $page = 1;
	var $max_pages = 0;
	var $offset = 0;
	var $cache_time;
	var $ext="";
	
	
	/**
	 * Constructor
	 *
	 * @param resource $connection Mysql connection link
	 * @param string $sql SQL query to paginate. Example : SELECT * FROM users
	 * @param integer $rows_per_page Number of records to display per page. Defaults to 10
	 * @param integer $links_per_page Number of links to display per page. Defaults to 5
	 * @param string $append Parameters to be appended to pagination links 
	 */
	
	function Pagination($sql, $rows_per_page = 10, $links_per_page = 5, $append = "",$time=10,$ext="") {
	
		$this->ext=$ext;
		$this->cache_time=$time;
		$this->sql = $sql;
		$this->rows_per_page = (int)$rows_per_page;
		if (intval($links_per_page ) > 0) {
			$this->links_per_page = (int)$links_per_page;
		} else {
			$this->links_per_page = 5;
		}
		$this->append = $append;
		$this->php_self = htmlspecialchars($_SERVER['PHP_SELF'] );
		if (isset($_GET['page'] )) {
			$this->page = intval($_GET['page'] );
		}
	}
	
	/**
	 * Executes the SQL query and initializes internal variables
	 *
	 * @access public
	 * @return resource
	 */
	function paginate($cache,$sql2) {
		//Load data from cache
		    // 1.Load Tong So Record
			$data=$cache->load($sql2,"pagination");
			if($data)
			{
			//------Lay tong so dong va ket qua;
			$this->total_rows=$data;
			}
			else
			{
				// 1. Load Result du tren limit
		//Neu load ko duoc se ket noi co so du lieu va luu data vao cache========
			
		$this->connect();// --------Conncect database-----------
		$this->query($sql2);
		$this->total_rows =$this->num_rows();
		$this->close();
		$cache->save($sql2,$this->total_rows,$this->cache_time,'pagination');
		}
		
		//Return FALSE if no rows found
		if ($this->total_rows == 0) {
			if ($this->debug)
			return FALSE;
		}
		
		//Max number of pages
		$this->max_pages = ceil($this->total_rows / $this->rows_per_page );
		if ($this->links_per_page > $this->max_pages) {
			$this->links_per_page = $this->max_pages;
		}
		
		//Check the page value just in case someone is trying to input an aribitrary value
		if ($this->page > $this->max_pages || $this->page <= 0) {
			$this->page = 1;
		}
		
		//Calculate Offset
		$this->offset = $this->rows_per_page * ($this->page - 1);
		$sql=$this->sql . " limit {$this->offset},{$this->rows_per_page}";
		$data2=$cache->load($sql,'pagination');
		if($data2)
		{	
			return $data2;
		}
		else
		{ 
		
		//Begin not cache condition
		//Fetch the required result set
		$this->connect();
		$this->query($sql);
		
		if ($this->num_rows()==0) {
			return false;
		}
		$data3;
		$i=0;
		
		if($this->ext=='post'){ // Truong hop dung de query lay post image,video,text from indexpost wwhere ext=post
			
		
					while($row=$this->fetch())
					{
						
							if($row['is_image']==1){
									$data3[$i++]=cache_get_post($row['post_id'],"post_image",$row['id']);
								
							}
							
							if($row['is_video']==1){
								$data3[$i++]=cache_get_post($row['post_id'],"post_media",$row['id']);
						}
							
							if($row['is_text']==1){
								$data3[$i++]=cache_get_post($row['post_id'],"post_text",$row['id']);
							
							}
							
					
						
					}
		
		
			
		
		}else
		if($this->ext=='album'){
				while($row=$this->fetch())
					{
							//query get link image -----
							$image=album_get_image($row['id']);
							$row['image']=$image;
							$data3[$i++]=$row;
					}
		
		}else
		{
			// Lay data phan trang paging binh thuong
					while($row=$this->fetch())
					{
							$data3[$i++]=$row;
					}
		}
		
		//End of lay du lieu, close ket noi
		$this->close();
		$cache->save($sql,$data3,$this->cache_time,'pagination');
		return $data3;
		} //end of not cache condtion
	}
	
		function paginate_nocache($sql2) {
		//Load data from cache
		    // 1.Load Tong So Record
		// 1. Load Result du tren limit
		//Neu load ko duoc se ket noi co so du lieu va luu data vao cache========
			
		$this->connect();// --------Conncect database-----------
		$this->query($sql2);
		$this->total_rows =$this->num_rows();
		$this->close();
		
		
		
		//Return FALSE if no rows found
		if ($this->total_rows == 0) {
			if ($this->debug)
			return FALSE;
		}
		
		//Max number of pages
		$this->max_pages = ceil($this->total_rows / $this->rows_per_page );
		if ($this->links_per_page > $this->max_pages) {
			$this->links_per_page = $this->max_pages;
		}
		
		//Check the page value just in case someone is trying to input an aribitrary value
		if ($this->page > $this->max_pages || $this->page <= 0) {
			$this->page = 1;
		}
		
		//Calculate Offset
		$this->offset = $this->rows_per_page * ($this->page - 1);
		$sql=$this->sql . " LIMIT {$this->offset}, {$this->rows_per_page}";
		

		
		//Begin not cache condition
		//Fetch the required result set
		$this->connect();
		$this->query($sql);
		if ($this->num_rows()==0) {
			return false;
		}
		$data3;
		$i=0;
		while($row=$this->fetch())
		{
			$data3[$i++]=$row;
		}
		$this->close();
		return $data3;
		} //end of not cache condtion
	

	
	
	
	/**
	 * Display the link to the first page
	 *
	 * @access public
	 * @param string $tag Text string to be displayed as the link. Defaults to 'First'
	 * @return string
	 */
	 		function renderOlderpost($tag = 'Older Post') {
		if ($this->total_rows == 0)
			return FALSE;
		
		if ($this->page < $this->max_pages) {
			return '
				<ul class="pager">
 			 <li class="previous">
			<a href="' . $this->append . ($this->page + 1) .'">&larr;' . $tag . '</a>
			</li>
			 </ul>
			';
			
		} else {
			return false;
		}
	}
	
		function renderNewerpost($tag = 'Newer Post') {
		if ($this->total_rows == 0)
			return FALSE;
		
		if ($this->page > 1) {
			return ' <ul class="pager">
 			 <li class="next"><a href="' . $this->append . ($this->page - 1) .'">' . $tag . ' &rarr;</a></li>
			 </ul>+';
		} else {
			return false;
		}
	}
	 
	function renderFirst($tag = '&lsaquo;&lsaquo;') {
		if ($this->total_rows == 0)
			return FALSE;
		
		if ($this->page == 1) {
			return false;
		} else {
			return '
			<li ><a href="' . $this->append .'1'.'">' . $tag . '</a></li>';
		
		}
	}
	
	/**
	 * Display the link to the last page
	 *
	 * @access public
	 * @param string $tag Text string to be displayed as the link. Defaults to 'Last'
	 * @return string
	 */
	function renderLast($tag = '&rsaquo;&rsaquo;') {
		if ($this->total_rows == 0)
			return FALSE;
		
		if ($this->page == $this->max_pages) {
			return false;
		} else {
			return '<li  ><a href="' . $this->append.$this->max_pages .'">' . $tag . '</a></li>';
		
		}
	}
	
	/**
	 * Display the next link
	 *
	 * @access public
	 * @param string $tag Text string to be displayed as the link. Defaults to '>>'
	 * @return string
	 */
	function renderNext($tag = '&rsaquo;') {
		if ($this->total_rows == 0)
			return FALSE;
		
		if ($this->page < $this->max_pages) {
			return '
			<li ><a href="' . $this->append.($this->page + 1) .''.'">' . $tag . '</a></li>';
		} else {
			return false;
		}
	}
	
	/**
	 * Display the previous link
	 *
	 * @access public
	 * @param string $tag Text string to be displayed as the link. Defaults to '<<'
	 * @return string
	 */
	function renderPrev($tag = '&lsaquo;') {
		if ($this->total_rows == 0)
			return FALSE;
		
		if ($this->page > 1) {
			return '
			<li ><a href="' . $this->append .($this->page - 1) .''.'">' . $tag . '</a></li>';

		} else {
			return false;
		}
	}
	
	/**
	 * Display the page links
	 *
	 * @access public
	 * @return string
	 */
	function renderNav($prefix = '<li >', $suffix = '</li> ') {
		if ($this->total_rows == 0)
			return FALSE;
		
		$batch = ceil($this->page / $this->links_per_page );
		$end = $batch * $this->links_per_page;
		if ($end == $this->page) {
			//$end = $end + $this->links_per_page - 1;
		//$end = $end + ceil($this->links_per_page/2);
		}
		if ($end > $this->max_pages) {
			$end = $this->max_pages;
		}
		$start = $end - $this->links_per_page + 1;
		$links = '';
		
		for($i = $start; $i <= $end; $i ++) {
			if ($i == $this->page) {
				$links .=  "<li  class='disabled'><a href='".url()."#'>" . $i . $suffix."</a>";
			} else {
				$links .= ' ' . $prefix . '<a href="' . $this->append . $i . '">' . $i . '</a>' . $suffix . ' ';
			}
		}
		
		return $links;
	}
	
	/**
	 * Display full pagination navigation
	 *
	 * @access public
	 * @return string
	 */
	function renderFullNav() {

	
		if($this->total_rows>$this->rows_per_page)
		return "<div><ul class='pagination'><li  class='disabled'><a href='".url()."#'>Pages ($this->max_pages)</a></li> ".$this->renderFirst() .$this->renderPrev(). $this->renderNav() . $this->renderNext().$this->renderLast()."</ul></div>";
	}
	
	/**
	 * Set debug mode
	 *
	 * @access public
	 * @param bool $debug Set to TRUE to enable debug messages
	 * @return void
	 */
	function setDebug($debug) {
		$this->debug = $debug;
	}
	
	function get_num_page(){
	return $this->max_pages;
	}
}
?>
